<?php
/**
 * 企业支付
 */
namespace Org\Util;
class WxTransfers{
	public $mch_appid;//appid
	public $mchid;//商户id
	public $partner_trade_no;//商户订单 号
	public $openid;//用户的openid
	public $check_name;//校验用户姓名选项  NO_CHECK：不校验真实姓名,FORCE_CHECK：强校验真实姓名,OPTION_CHECK：针对已实名认证的用户才校验真实姓名
	public $re_user_name;//收款用户姓名
	public $amount;//金额
	public $key;//支付密匙
	public $spbill_create_ip;//调用接口的机器Ip地址
	public $desc;//调用接口的机器Ip地址
	public $post_url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';//接口地址
	
 	public function __construct($mch_appid,$mchid,$partner_trade_no,$openid,$check_name,$re_user_name,$amount,$desc,$key) {
        $this->mch_appid = $mch_appid;
        $this->mchid = $mchid;
        $this->partner_trade_no = $partner_trade_no;
        $this->openid = $openid;
        $this->check_name =$check_name;
        $this->re_user_name =$re_user_name;
        $this->amount =$amount;
        $this->desc =$desc;
        $this->key=$key;
    }
     /**
     * 
     * 生成提交参数
     */
    public function setParam(){
    	$param['mch_appid'] =$this->mch_appid; 
    	$param['mchid'] =$this->mchid; 
    	$param['nonce_str'] =$this->createNoncestr(); 
    	$param['partner_trade_no'] =$this->partner_trade_no; 
    	$param['openid'] =$this->openid; 
    	$param['check_name'] =$this->check_name; 
    	if ($param['check_name']!='NO_CHECK'){
    		$param['re_user_name'] =$this->re_user_name; 
    	}
    	$param['amount'] =$this->amount; 
    	$param['desc'] =$this->desc; 
    	$param['spbill_create_ip'] =get_client_ip(); 
    	$param['sign'] =$this->create_sign($param); 
    	$xml = $this->arrayToXml($param);
    	return $xml;
    }
	/**
     * 
     * POST提交数据
     */
    public function ssl_post($xml){
    	$ch = curl_init();
    	curl_setopt($ch,CURLOPT_URL,$this->post_url);
    	curl_setopt($ch,CURLOPT_HEADER,0);
    	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    	curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    	curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
    	curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/apiclient_cert.pem');
    	curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
	    curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/apiclient_key.pem');
	    curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
	    curl_setopt($ch,CURLOPT_CAINFO,getcwd().'/rootca.pem');
	    curl_setopt($ch,CURLOPT_POST, 1);
	    curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
	    $data = curl_exec($ch);
		$data =  $this->xmlToArray($data);
		if ($data['result_code'] =='SUCCESS'){
			$r_data['status'] = 1;
			$r_data['payment_no'] = $data['payment_no'];
			return $r_data;
		}else{
			$r_data['status'] = 0;
			$r_data['error'] = $data['return_msg'].'/'.$data['err_code_des'];
			return $r_data;
		}
    }
/**
	 * 
	 * 生成认证签名
	 * create_time 2015-10-12
 	 * author wj   
	 */
	public function create_sign($data){
		$str ='';
		ksort($data);//数组排序
		foreach($data as $k=>$v){//拼接成相关规则的字符串
			if (!empty($v)){
				if ($str==''){
					$str =$k . "=" . $v ;
				}else{
					$str .='&'.$k . "=" . $v ;
				}
			}
		}
		$str .="&key=$this->key";
		//加密
		$str = MD5($str);
		$res = strtoupper($str);
		return $res;
	}
	/**
	 * 	作用：产生随机字符串，不长于32位
	 */
	public function createNoncestr( $length = 32 ){
		$chars = "abcdefghijklmnopqrstuvwxyz0123456789";  
		$str ="";
		for ( $i = 0; $i < $length; $i++ )  {  
			$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);  
		}  
		return $str;
	}
	/**
	 * 	作用：array转xml
	 */
	function arrayToXml($arr)
    {
        $xml = "<xml>";
        foreach ($arr as $key=>$val)
        {
        	 if (1==1)
        	 {
        	 	$xml.="<".$key.">".$val."</".$key.">"; 

        	 }
        	 else
        	 	$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";  
        }
        $xml.="</xml>";
        return $xml; 
    }
	/**
	 * 	作用：将xml转为array
	 */
	public function xmlToArray($xml)
	{		
        //将XML转为array        
        $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
		return $array_data;
	}
}
?>